package com.mysen.sso.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mysen.model.Pagenation;
import com.mysen.model.Response;
import com.mysen.sso.cache.SessionCache;
import com.mysen.sso.model.Permission;
import com.mysen.sso.model.Role;
import com.mysen.sso.model.User;
import com.mysen.sso.service.RoleService;

@Controller
public class RoleController {
	
	@Reference
	private RoleService service;
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private SessionCache sessionCache;
	
	@PostMapping("/role")
	@ResponseBody
	public Response<Role> saveRole(@RequestBody Role role){
		Response<Role> resp = new Response<Role>();
		
		User curUser = sessionCache.getUser(request);
		role.setCuser(curUser.getUserId());
		
		role = service.save(role);
		resp.setSuccess(true);
		resp.setData(role);
		return resp;
	}
	
	@PostMapping("/roles")
	@ResponseBody
	public Response<?> queryByPage(@RequestBody Role role){
		Pagenation page = role.getPage();
		if(page == null) {
			page = new Pagenation(0, 10);
			role.setPage(page);
		}
		return service.selectByPage(role);
	}
	
	@PutMapping("/role/{id}")
	@ResponseBody
	public Response<?> updateRole(@RequestBody Role role, @PathVariable("id") String roleId){
		Response<Role> resp = new Response<Role>();
		role.setRoleId(roleId);
		service.updateById(role);
		resp.setData(role);
		resp.setSuccess(true);
		return resp;
	}
	
	@GetMapping("/role/{id}")
	@ResponseBody
	public Response<Role> getRole(@PathVariable("id") String roleId){
		Response<Role> resp = new Response<Role>();
		resp.setData(service.selectById(roleId));
		resp.setSuccess(true);
		return resp;
	}
	
	@DeleteMapping("/role/{id}")
	@ResponseBody
	public Response<Role> delete(@PathVariable("id") String roleId){
		Response<Role> resp = new Response<Role>();
		service.deleteById(roleId);
		resp.setSuccess(true);
		return resp;
	}

	@PostMapping("/role/permissions/{id}")
	@ResponseBody
	public Response<Role> addPermission(@PathVariable("id") String roleId, Set<Permission> permissions){
		Response<Role> resp = new Response<Role>();
		Role role = new Role(roleId);
		User curUser = sessionCache.getUser(request);
		role.setCuser(curUser.getUserId());
		role.setPermissions(permissions);
		service.addPermissions(role);
		resp.setSuccess(true);
		return resp;
	}
	
	@GetMapping("/roles")
	@ResponseBody
	public Response<?> getRoleTree(@RequestParam("pId") String pId, @RequestParam("isAll") boolean isAll){
		List<Role> tree = service.selectTree(pId, isAll);
		Response<?> resp = new Response<>();
		resp.setDatas(tree);
		resp.setSuccess(true);
		return resp;
	}
}
